<template>
  <el-form :model="ruleForm" 
    status-icon :rules="rules" 
    ref="ruleForm" label-width="100px" 
    class="demo-ruleForm"
    style="width:400px">
        <el-form-item label="原始密码" prop="oldpass">
            <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>  
</template>

<script>
import {strToMd5} from '../../util/md5.js'
export default {
    name:"ChangepasswordForm",
    data(){
        var validateOldPass =(rule, value,callback)=>{
            // const password=window.sessionStorage.getItem('password')
            // this.ruleForm.oldpass=strToMd5(this.ruleForm.oldpass)
            if (value === ''){
            callback(new Error('请输入密码'));
            } 
            // else if(password!==this.ruleForm.oldpass){
            //      callback(new Error('密码输入错误'));
            // }
            callback();
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请输入密码'));
            } else {
            if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
            } else {
            callback();
            }
        };
        return {
            ruleForm: {
                oldpass:'',
                pass: '',
            },
            rules: {
                oldpass:[
                { validator: validateOldPass, trigger: 'blur' },
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                pass: [
                { validator: validatePass, trigger: 'blur' },
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                checkPass: [
                { validator: validatePass2, trigger: 'blur' },
                { required: true, message: '请再次输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                
            }
        }
    },data(){
        var validateOldPass =(rule, value,callback)=>{
            // const password=window.sessionStorage.getItem('password')
            // this.ruleForm.oldpass=strToMd5(this.ruleForm.oldpass)
            if (value === ''){
            callback(new Error('请输入密码'));
            } 
            // else if(password!==this.ruleForm.oldpass){
            //      callback(new Error('密码输入错误'));
            // }
            callback();
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请输入密码'));
            } else {
            if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
            } else {
            callback();
            }
        };
        return {
            ruleForm: {
                oldpass:'',
                pass: '',
            },
            rules: {
                oldpass:[
                { validator: validateOldPass, trigger: 'blur' },
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                pass: [
                { validator: validatePass, trigger: 'blur' },
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                checkPass: [
                { validator: validatePass2, trigger: 'blur' },
                { required: true, message: '请再次输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ],
                
            }
        }
    },
    methods:{
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        submitForm() {
            this.$refs.ruleForm.validate(async valid=> {
            const token=window.sessionStorage.getItem('token')
            this.ruleForm.oldpass=strToMd5(this.ruleForm.oldpass)
            const password=window.sessionStorage.getItem('password')
            this.ruleForm.pass=strToMd5(this.ruleForm.pass)
            const pass=this.ruleForm.pass
            if(password==this.ruleForm.oldpass){
                const {data:res}=await this.$http.post("/javaweb/change",{pass,token});
                if(res.code!=200)return this.$message.error('修改失败！');
                this.$message.success('修改成功！');
                this.$router.push("/login");
            }
            else{
                return 
            }
            });
        },
    }
}
</script>

<style>

</style>